﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using Phoenix.Service.Contracts;
using Phoenix.Service;
using Phoenix.Service.Model;

namespace GetCategory
{
    //http://www.specl.com/public/assort.shtml
	class Program
	{
		static void Main(string[] args)
		{
			GetUrl g = new GetUrl();
            g.GetoneCate();
		}
	}

	class GetUrl
	{
		ICategoryService ICategory = new CategoryService();
        IDCreaterService IIDCreater = new IDCreaterService();

		public void GetoneCate()
		{
            WebRequest req = WebRequest.Create("http://www.specl.com/public/assort.shtml");
			req.Method = "get";
			WebResponse result = req.GetResponse();
			Stream sm= result.GetResponseStream();
			StreamReader sr = new StreamReader(sm,Encoding.GetEncoding("utf-8"));
			string html= sr.ReadToEnd().Replace("\r\n","");

			Regex reg = new Regex("<div class=\"assort_left\">(.*?)<div class=\"assort_right\">");

            html=reg.Match(html).Groups[1].Value;

            Regex reg2 = new Regex("<h3>.*?>(.*?)</a></h3>(.*?)</li>      </ul>");




			MatchCollection mc= reg2.Matches(html);
			for (int i = 0; i < mc.Count; i++)
			{
                string firstHtmlCate = mc[i].Value;

                Regex firstReg = new Regex("<h3>.*?>(.*?)</a></h3>");

                string firstName = firstReg.Match(firstHtmlCate).Groups[1].Value;

                CategoryModel firstModel = new CategoryModel();
                firstModel.CategoryID = IIDCreater.GetNewID("CategoryID", "分类编号", 1000);
                firstModel.CategoryName = firstName;
                firstModel.CateStatus = 0;
                firstModel.CateAleph = "";
                firstModel.IDPath = firstModel.CategoryID.ToString(); 
                firstModel.NamePath = firstName;
                firstModel.ParentId = 0;
                ICategory.AddCategory(firstModel);


                 Regex secondHtmlReg=new Regex("<h4>.*?>.*?</a></h4>.*?<div class=\"clear\">");

                 MatchCollection mc2 = secondHtmlReg.Matches(firstHtmlCate);

                 for (int j = 0; j < mc2.Count; j++)
                 {
                     Regex secondNameReg = new Regex("<h4>.*?>(.*?)</a></h4>");

                     string secondName = secondNameReg.Match(mc2[j].Value).Groups[1].Value;


                     Regex thirdNameReg = new Regex("<a href=.*?>(.*?)</a>");

                     Regex thirdHtmlReg = new Regex("<div class=\"as_con1\">.*?</div");

                     string thirdHtml = thirdHtmlReg.Match(mc2[j].Value).Value;

                     MatchCollection mc3 = thirdNameReg.Matches(thirdHtml);

                     CategoryModel secondModel = new CategoryModel();
                     secondModel.CategoryID = IIDCreater.GetNewID("CategoryID", "分类编号", 1000);
                     secondModel.CategoryName = secondName;
                     secondModel.CateStatus = 0;
                     secondModel.CateAleph = "";
                     secondModel.IDPath = firstModel.CategoryID.ToString() + "-" + secondModel.CategoryID.ToString();
                     secondModel.NamePath = firstModel.CategoryName + "-" + secondName;
                     secondModel.ParentId = firstModel.CategoryID;
                     ICategory.AddCategory(secondModel);



                     for (int k = 0; k < mc3.Count; k++)
                     {
                         string thirdName = thirdNameReg.Match(mc3[k].Value).Groups[1].Value;

                         CategoryModel thirdModel = new CategoryModel();
                         thirdModel.CategoryID = IIDCreater.GetNewID("CategoryID", "分类编号", 1000);
                         thirdModel.CategoryName = thirdName;
                         thirdModel.CateStatus = 0;
                         thirdModel.CateAleph = "";
                         thirdModel.IDPath = firstModel.CategoryID.ToString() + "-" + secondModel.CategoryID.ToString() + "-" + thirdModel.CategoryID.ToString();
                         thirdModel.NamePath = firstModel.CategoryName + "-" + secondModel.CategoryName + "-" + thirdName;
                         thirdModel.ParentId = secondModel.CategoryID;
                         ICategory.AddCategory(thirdModel);
                     }






                 }










                //CategoryModel model = new CategoryModel()
                //{
                //    CateAleph="",
                //    CategoryName=mc[i].Groups[2].Value,
                //    Url = "http://www.cntcw.cn/" + mc[i].Groups[1].Value,
                //    CategoryType=1,
                //    CateStatus=1,
                //    IsLeaf=0,
                //    ParentId=0
                //};
                //ICategory.AddCategory(model);

			}
		}

        public void GetTwoCate()
        {

        }

        public void GetThreCate()
        {

        }

        public void GetSubCate()
        {
            WebRequest req = WebRequest.Create("http://www.cntcw.cn/tclist.aspx?tid=134&l=2&xs=1");
            req.Method = "get";
            WebResponse result = req.GetResponse();
            Stream sm = result.GetResponseStream();
            StreamReader sr = new StreamReader(sm, Encoding.GetEncoding("gb2312"));
            string html = sr.ReadToEnd();

            Regex reg = new Regex("<div class=\"lb_right\"><ul><li style=\".*?>(.*)</a>");
            Match ma= reg.Match(html);
            html = ma.Groups[1].Value;

            reg = new Regex("<a href=\"(.*?)\">(.*?)</a>");

            MatchCollection mc = reg.Matches(html);
            for (int i = 0; i < mc.Count; i++)
            {

                CategoryModel model = new CategoryModel()
                {
                    //CateAleph = "",
                    //CategoryName = mc[i].Groups[2].Value,
                    //Url = "http://www.cntcw.cn/" + mc[i].Groups[1].Value,
                    //CategoryType = 1,
                    //CateStatus = 1,
                    //IsLeaf = 0,
                    ParentId = 11
                };
                ICategory.AddCategory(model);

            }

        }
	}
}
